//
//  WPNewFeatureView.swift
//  WB
//
//  Created by 汪鹏 on 2017/1/4.
//  Copyright © 2017年 汪鹏. All rights reserved.
//

import UIKit

class WPNewFeatureView: UIView {

    // 进入微博按钮
    @IBOutlet weak var goToStatus: UIButton!
    // 页码
    @IBOutlet weak var pageControl: UIPageControl!
    // scrollView
    @IBOutlet weak var scrollView: UIScrollView!
    
    
    override func awakeFromNib() {
        
        // 设置scrollView的内容
        let count = 4
        
        let frame = UIScreen.main.bounds
        
        for i in 0..<count {
           
            let image = UIImage(named: "new_feature_\(i+1)")
            
            let imageView = UIImageView(image: image)
            
            imageView.frame = frame.offsetBy(dx: CGFloat(i) * frame.width, dy: 0)
            
            scrollView.addSubview(imageView)
            
        }
        
        scrollView.contentSize = CGSize(width: CGFloat(count + 1) * frame.width, height: 0)
        
        scrollView.delegate = self
        
        scrollView.isPagingEnabled = true
        
        scrollView.bounces = false
        
        scrollView.showsVerticalScrollIndicator = false
        
        scrollView.showsHorizontalScrollIndicator = false
        
        goToStatus.isHidden = true
        
    }
    
    /// 类方法快速创建新特性界面
    class func newFeatureView() -> WPNewFeatureView {
        
        let nib = UINib(nibName: "WPNewFeatureView", bundle: nil)
        
        let newFeature = nib.instantiate(withOwner: nil, options: nil)[0] as! WPNewFeatureView
        
        // 从xib加载的视图默认大小都是600*600
        newFeature.frame = UIScreen.main.bounds
        
        return newFeature
    }

    // 进入微博
    @IBAction func goToStatus(_ sender: Any) {
        
        self.removeFromSuperview()
    }
}

// MARK:--UIScrollViewDelegate
extension WPNewFeatureView: UIScrollViewDelegate {
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        let page = Int(scrollView.contentOffset.x / scrollView.frame.width + 0.7)
        
        pageControl.currentPage = page
        
        pageControl.isHidden = page == scrollView.subviews.count
            
        goToStatus.isHidden = page != scrollView.subviews.count-1
        
    }
    
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        let page = Int(scrollView.contentOffset.x / scrollView.frame.width + 0.7)
        
        if page == scrollView.subviews.count {
            
            self.removeFromSuperview()
        }
        
    }
    
}
